package com.xl6667.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xl6667.constants.SystemConstants;
import com.xl6667.domain.ResponseResult;
import com.xl6667.domain.dto.CategoryDto;
import com.xl6667.domain.dto.PageDto;
import com.xl6667.domain.entity.Article;
import com.xl6667.domain.entity.Category;
import com.xl6667.mapper.CategoryMapper;
import com.xl6667.service.ArticleService;
import com.xl6667.service.CategoryService;
import com.xl6667.util.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 熊磊
* @description 针对表【sg_category(分类表)】的数据库操作Service实现
* @createDate 2024-03-14 16:56:24
*/
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Autowired
    private ArticleService articleService;
    @Override
    public ResponseResult getCategoryList() {
        //查询有文章存在的分类Id
        LambdaQueryWrapper<Article> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Long> arcCateList = articleService.list(queryWrapper1)
                .stream().map(article -> article.getCategoryId())
                .collect(Collectors.toList());
        //查询分类
        List<Category> categories = listByIds(arcCateList);
        List<CategoryDto> categoryDtos = BeanCopyUtil.copyBeanList(categories, CategoryDto.class);

        return ResponseResult.okResult(categoryDtos);
    }

    @Override
    public ResponseResult listAllCategory() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Category::getStatus,SystemConstants.STATUS_NORMA);
        queryWrapper.eq(Category::getDelFlag,SystemConstants.DEL_FLAG_NORMA);

        List<Category> categoryList = list(queryWrapper);
        List<CategoryDto> categoryDtos = BeanCopyUtil.copyBeanList(categoryList, CategoryDto.class);
        return ResponseResult.okResult(categoryDtos);
    }

    @Override
    public ResponseResult listCategory(Integer pageNum, Integer pageSize, String name, String status) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name),Category::getName,name);
        queryWrapper.eq(StringUtils.hasText(status),Category::getStatus,status);

        Page<Category> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<Category> records = page.getRecords();

        PageDto pageDto = new PageDto(records,page.getTotal());
        return ResponseResult.okResult(pageDto);

    }

    @Override
    public ResponseResult changeStatusCategory(Category category) {
        updateById(category);
        return ResponseResult.okResult("修改成功");
    }

    @Override
    public ResponseResult addCategory(Category category) {
        save(category);
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult delCAtegory(List<Long> ids) {
        LambdaUpdateWrapper<Category> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Category::getId,ids).set(Category::getDelFlag,1);
        update(null,updateWrapper);
        return ResponseResult.okResult("删除成功");
    }

    @Override
    public ResponseResult getCategory(Long id) {
        Category category = getById(id);
        return ResponseResult.okResult(category);
    }

    @Override
    public ResponseResult putCategory(Category category) {
        updateById(category);
        return ResponseResult.okResult("修改成功");
    }
}




